package com.example.module.api;

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.util.Log;

import com.example.util.FileUtil;

public abstract class AsyncHttpTask extends AsyncTask<APIRequest, Integer, HttpTaskReponse> {
	private static final String TAG = "AsyncHttpTask";
	
	protected abstract HttpTaskReponse doRequest(APIRequest request) throws Exception;
	
	public static final int RESPONSE_OK = 0;
	public static final int RESPONSE_FAIL = 1;
	
	protected Context mContext;
    private PowerManager.WakeLock mWakeLock;
    private ProgressDialog mProgressDialog;
    private HttpReponseListener mHttpReponseListener;
    protected String mSessionKey;
    
	protected HttpClient client = null;
//	HttpContext httpContext = new BasicHttpContext();

    public interface HttpReponseListener {
    	public void OnResponse(HttpTaskReponse response);
    }
    
    public AsyncHttpTask(Context context) {
    	this.mContext = context;
		this.client = new DefaultHttpClient();
	}

	@Override
    protected void onPreExecute() {
    	super.onPreExecute();
		
    	PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
             getClass().getName());
        mWakeLock.acquire();
        
        mProgressDialog = new ProgressDialog(mContext);
		mProgressDialog.setTitle("Communicate to API");
		mProgressDialog.setMessage("waiting for response...");
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgressDialog.setCancelable(true);
        mProgressDialog.show();
    }
    
    @Override
    protected void onProgressUpdate(Integer... progress) {
    	super.onProgressUpdate(progress);
    	
    	mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgress(progress[0]);
    }
    
    @Override
    protected void onPostExecute(HttpTaskReponse result) {
    	super.onPostExecute(result);
    	
    	mWakeLock.release();
        mProgressDialog.dismiss();
        
        if (mHttpReponseListener != null)
    		mHttpReponseListener.OnResponse(result);
    }
    
	@Override
	protected HttpTaskReponse doInBackground(APIRequest... requests) {
		publishProgress((int) 2, (int) 4);
		
		// TODO check Network status
		
		publishProgress((int) 6, (int) 10);
		
		try {
			return doRequest(requests[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
			return new HttpTaskReponse(RESPONSE_FAIL, e.toString());
		}		
	}

	public HttpReponseListener getmHttpReponseListener() {
		return mHttpReponseListener;
	}

	public void setmHttpReponseListener(HttpReponseListener mHttpReponseListener) {
		this.mHttpReponseListener = mHttpReponseListener;
	}
	
	public String toImageFile(HttpEntity resEntity, String cardToken) {
		if (resEntity == null)
			return null;
		
		try {
			Bitmap bitmap = BitmapFactory.decodeStream((InputStream) resEntity.getContent());
			String fileName = FileUtil.getAlbumStorageDir("CardImage") + "/" + String.format("%s.png", cardToken);
			
			Log.d("doSync", "fileName=" + fileName);
			
			FileUtil.saveBitmap(bitmap, fileName);
			Log.d("post", "fileName=" + fileName);
			
			return fileName;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
